<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\api\controller\system;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use app\admin\model\FileUpload;
use app\admin\model\FileCategory;
use think\Model;

/**
 * @ControllerAnnotation(title="上传文件管理")
 * Class Uploadfile
 *
 * @package app\admin\controller\system
 */
class UploadFile extends AdminController
{
    
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new FileUpload();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $user_id = $request->_UID;
        $tenant_id = $request->_TENANTID;
        
        $page = input("page");
        $limit = input("limit");
        
        $where[] = [
            "tenant_id",
            "=",
            $tenant_id
        ];
        

            $where[] = [
                "user_id",
                "=",
                $user_id
            ];

        
        if (! empty(input('cid'))) {
            $where[] = [
                "cid",
                "=",
                input('cid')
            ];
        }
        
        if (! empty(input('type'))) {
            $where[] = [
                "type",
                "=",
                input('type')
            ];
        }
        
        $count = $this->model->where($where)->count();
        
        if (! empty($page) && ! empty($limit)) {
            $list = $this->model->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
        } else {
            $list = $this->model->where($where)
                ->order($this->sort)
                ->select();
        }
        
        return msg([
            "list" => $list,
            "count" => $count
        ], 0, "success");
    }

    /**
     * @NodeAnotation(title="删除文件")
     */
    public function delFile()
    {
        $request = $this->request;
        $user_id = $request->_UID;
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                
                $ids = $post['ids'];
                $temp1 = FileUpload::where("id", "in", $ids)->delete(true);
            } catch (\Exception $e) {
                return msg([], 1, "删除失败");
            }
            return msg([], 0, "删除成功");
        }
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function cateList()
    {
        $request = $this->request;
        $user_id = $request->_UID;
        $tenant_id = $request->_TENANTID;
        
        $page = input("page");
        $limit = input("limit");
        
        $where[] = [
            "tenant_id",
            "=",
            $tenant_id
        ];
        
        if (! empty(input('pid'))) {
            $where[] = [
                "pid",
                "=",
                input('pid')
            ];
        }
        
        $count = FileCategory::where($where)->count();
        
        if (! empty($page) && ! empty($limit)) {
            $list = FileCategory::where($where)->field('name title,id,pid')
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
        } else {
            $list = FileCategory::where($where)->field('name title,id,pid')
                ->order($this->sort)
                ->select();
        }
        
        return msg($list, 0, "success");
    }

    public function addCate()
    {
        $request = $this->request;
        $user_id = $request->_UID;
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['name'] = $post['title'];
                $post['user_id'] = $user_id;
                $id = $post['id'];
                unset($post['title']);
                if (! empty($id)) {
                    if ($post['pid'] == $post['id']) {
                        return msg([], 1, "上级不能为自己");
                    }
                    $save = FileCategory::where("id", $post['id'])->update($post);
                } else {
                    FileCategory::insert($post);
                }
            } catch (\Exception $e) {
                return msg([], 1, "保存异常" . $e->getMessage());
            }
            return msg([], 0, "保存成功");
        }
    }

    /**
     * @NodeAnotation(title="删除分类")
     */
    public function delCate()
    {
        $request = $this->request;
        $user_id = $request->_UID;
        $id = $request->param("id");
        Db::startTrans();
        try {
            $temp = FileCategory::where("id", $id)->delete(true);
            
            $temp1 = FileUpload::where("cid", $id)->delete(true);
            
            Db::commit();
            return msg([], 0, "删除成功");
        } catch (\Exception $th) {
            Db::rollback();
            return msg([], 1, "删除失败 : " . $th->getMessage());
        }
    }
}